// ==UserScript==
// @name         zha-nianshou
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  sorry, nianshou, I'm late
// @author       Coolman <QQ Group ID: 892850160>
// @match        https://bunearth.m.jd.com/babelDiy/SGFJVMOZADGTQCZWGEYU/4PWgqmrFHunn8C38mJA712fufguU/*
// @match        https://gitee.com/Coolman/55a14dab8766eedda13268e931a4d09e/
// @grant        none
// ==/UserScript==
Object.defineProperty(navigator, 'userAgent', {
    value: atob('amRhcHA7YW5kcm9pZDs4LjMuMDsxMDtkNDFkOGNkOThmMDBiMjA0O25ldHdvcmsvd2lmaTttb2RlbC9NSSA2O2FkZHJlc3NpZC81NDEyODY2NzI7YWlkL2Q0MWQ4Y2Q5OGYwMGIyMDQ7b2FpZC87b3NWZXIvMjk7YXBwQnVpbGQvNjk5MDk7cHNuL2Q0MWQ4Y2Q5OGYwMGIyMDR8ODA7cHNxLzI7dWlkL2Q0MWQ4Y2Q5OGYwMGIyMDQ7YWRrLzthZHMvO3BhcC9KQTIwMTVfMzExMjEwfDguMy4wfEFORFJPSUQgMTA7b3N2LzEwO3B2Lzc5LjI7amR2LzB8YW5kcm9pZGFwcHx0XzMzNTEzOTc3NHxhcHBzaGFyZXxRUWZyaWVuZHN8MTU3MzAzMTc1ODAwNHwxNTczMDMxNzU4O3JlZi9jb20uamluZ2RvbmcuYXBwLm1hbGwuaG9tZS5KREhvbWVGcmFnbWVudDtwYXJ0bmVyL2dvb2dsZTthcHBycGQvSG9tZV9NYWluO01vemlsbGEvNS4wIChMaW51eDsgQW5kcm9pZCAxMDsgTUkgNiBCdWlsZC9RUDFBLjE5MDcxMS4wMjA7IHd2KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBWZXJzaW9uLzQuMCBDaHJvbWUvNzQuMC4zNzI5LjE4NiBNb2JpbGUgU2FmYXJpLzUzNy4zNg'),
});
(() => {
    console.log('hello zha-nianshou!');
    if (!window.webviewVisible) {
        console.info('invaild script, pls contact the author!');
        return;
    }
    // https://bunearth.m.jd.com/babelDiy/UXSSATLAQZAGQSGBDGBA/4PWgqmrFHunn8C38mJA712fufguU/index.html?shareType=taskHelp&inviteId=RlI2EYE5GXAKaPBqpmagYuOYYoxmFMPMm6cK1TZoRNvyvA9g3xgSHIc&taskId=1&itemId=AQW0HwfrMlGRWQDmm3HVDlg
    const exclusions = ['邀请好友助力', '开启LBS定位', '逛年货节主会场',];
    const reg = new RegExp(exclusions.join('|'));
    const getRndInteger = (min, max) => {
        return Math.floor(Math.random() * (max - min) ) + min;
    };
    const getNodes = async () => {
        const lists = document.querySelectorAll('li.bym_task_list');
        if (!lists.length) {
            console.info('not found task list');
            return;
        }
        let todo = [];
        lists.forEach((item) => {
            !reg.test(item.innerText) && todo.push(item);
        });
        const done = document.querySelectorAll('.bym_task_list_btn_done');
        if (done.length == todo.length) {
            console.info('*** all done ***')
            return;
        }
        for (let list of todo) {
            if (!checkIfDone(list)) {
                goTask(list);
                await sleep(getRndInteger(1000, 1500));
                goaway();
                await sleep(getRndInteger(2000, 3500));
                comeback();
            }
        }
        getNodes();
    };
    const goTask = (node) => {
        const taskName = node.querySelector('.bym_task_list_name_title');
        const taskBtn = node.querySelector('.bym_task_list_btn');
        taskName && (console.log(`--- ${ taskName.innerText } ---`));
        // taskBtn && taskBtn.click();
    };
    const checkIfDone = (node) => {
        return !!node.querySelector('.bym_task_list_btn_done');
    };
    const goaway = () => {
        console.log('goaway');
        // webviewVisible(0);
    };
    const comeback = () => {
        console.log('comeback');
        // webviewVisible(!0);
    };
    const sleep = (ms) => {
        return new Promise(resolve => setTimeout(resolve, ms));
    };
    setTimeout(() => {
        const btn = document.querySelector('a.bym_home_btn_main');
        btn && btn.click();
        setTimeout(getNodes, 1000);
    }, 2000);
})();
